#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(&mFtpAccMgr, SIGNAL(uploadProgressSignal(qint64,qint64)),
            this, SLOT(uploadProgressSlot(qint64,qint64)));

    connect(&mFtpAccMgr, SIGNAL(downloadProgressSignal(qint64,qint64)),
            this, SLOT(downloadProgressSlot(qint64,qint64)));

    connect(&mFtpAccMgr, SIGNAL(netErrorSignal(QNetworkReply::NetworkError)),
            this, SLOT(netErrorSlot(QNetworkReply::NetworkError)));

    connect(ui->selectUploadPushButton, &QPushButton::clicked, this, &Widget::on_selectUploadPushButton_clicked);
}

Widget::~Widget()
{
    delete ui;
}


bool Widget::getHostInfoFromUi()
{
    ftpServerHost = ui->lineEditHostAddress->text();
    ftpServerPort = ui->lineEditPort->text().toInt();
    ftpServerUsr = ui->lineEditUsrName->text();
    ftpServerPasswd = ui->lineEditPassWord->text();

    if(ftpServerHost.isEmpty() || ftpServerUsr.isEmpty()
        || ftpServerPasswd.isEmpty())
    {
        QMessageBox::warning(this, "Host Error", "Please input Host Info:Host Usr PWD");
        return false;
    }
    return true;
}

void Widget::on_selectUploadPushButton_clicked()
{
    QDialog *dialog = new QDialog;
    dialog->show();
}

void Widget::on_pushButtonUpload_clicked()
{
    if(! getHostInfoFromUi())
        return;

    mFtpAccMgr.setHostPortInfo(ftpServerHost, ftpServerPort);
    mFtpAccMgr.setUserPwdInfo(ftpServerUsr, ftpServerPasswd);

    QString clientFile = QFileDialog::getOpenFileName(this,
                                                      QString::fromLocal8Bit("选择需要上传的文件"));
    if(clientFile.isEmpty())
    {
        QMessageBox::warning(this, "upload error", "please select One file");
        return;
    }


    QString serverPath = ui->lineEditUploadPath->text();
    if(serverPath.isEmpty())
    {
        QMessageBox::warning(this, "upload error", "please select correct ftp path");
        return;
    }

    mFtpAccMgr.putFileFromClent(clientFile, serverPath);
}


void Widget::on_pushButtonDownload_clicked()
{
    if(! getHostInfoFromUi())
        return;

    QString downloadPath = ui->lineEditDownloadPath->text();

    mFtpAccMgr.setHostPortInfo(ftpServerHost, ftpServerPort);
    mFtpAccMgr.setUserPwdInfo(ftpServerUsr, ftpServerPasswd);

    QString targetFileName = QFileInfo(downloadPath).fileName();

    if(targetFileName.isEmpty())
        return;

    QString saveNameWithPath = QFileDialog::getSaveFileName(this,
                                                            QString::fromLocal8Bit("保存为..."),
                                                            targetFileName,
                                                            "");


    mFtpAccMgr.getFileFromServer(saveNameWithPath, downloadPath);
}

void Widget::uploadProgressSlot(qint64 bytesReceived, qint64 bytesTotal)
{
    qint32 fileSize = (qint32)(bytesReceived / (1024 * 1024));
    if(bytesReceived > 0 && fileSize == 0)
    {
        fileSize = 1;
    }

    qint32 totalSize = (qint32)(bytesTotal / (1024 * 1024));
    if(bytesTotal > 0 && totalSize == 0)
    {
        totalSize = 1;
    }

    ui->progressBarUpload->setMaximum(totalSize);
    ui->progressBarUpload->setValue(fileSize);
}

void Widget::downloadProgressSlot(qint64 bytesReceived, qint64 bytesTotal)
{
    qint32 fileSize = (qint32)(bytesReceived / (1024 * 1024));
    if(bytesReceived > 0 && fileSize == 0)
    {
        fileSize = 1;
    }

    qint32 totalSize = (qint32)(bytesTotal / (1024 * 1024));
    if(bytesTotal > 0 && totalSize == 0)
    {
        totalSize = 1;
    }

    ui->progressBarDownload->setMaximum(totalSize);
    ui->progressBarDownload->setValue(fileSize);
}

void Widget::netErrorSlot(QNetworkReply::NetworkError errorCode)
{
    //显示在状态栏里
}
